package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.service.IHdtemplateService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 
 * </p>
 *
 * @author chenxuebin
 * @since 2020-04-25
 */
@Controller
public class HdtemplateController {

    private static final Logger logger = LoggerFactory.getLogger(HdtemplateController.class);

    @Autowired
    private IHdtemplateService hdtemplateService;

    @RequestMapping("admin/hdtemplate/index")
    public String index(Model model) {
        return "admin/hdtemplate";
    }

    @RequestMapping("/admin/hdtemplate/list")
    @ResponseBody
    public Page<Hdtemplate> list(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Hdtemplate> wrapper = new EntityWrapper<Hdtemplate>();
        wrapper.eq("mid", ObjectUtil.getMid());
        if (!Strings.isNullOrEmpty(search))
            wrapper.andNew().like("title", search);//.or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<Hdtemplate> page = hdtemplateService.selectPage(new Page<Hdtemplate>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/hdtemplate/add")
    public String add(@Valid Hdtemplate bean) {

        if(!Strings.isNullOrEmpty(bean.getYxtags())) bean.setYxtags(bean.getYxtags().replace("；",";").replace("：",";"));
        bean.setCreatetime(new Date());
        bean.setCreator(ObjectUtil.getUid());
        bean.setState(1);
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.insert();
        return "admin/hdtemplate";
    }

    @RequestMapping("/admin/hdtemplate/getHdtemplateById")
    @ResponseBody
    public Hdtemplate getHdtemplateById(long id) {
        Hdtemplate hdtemplate = hdtemplateService.selectById(id);
        return hdtemplate;
    }

    @RequestMapping("/admin/hdtemplate/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = hdtemplateService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/hdtemplate/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Hdtemplate bean = new Hdtemplate();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        hdtemplateService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/hdtemplate/modify")
    public String update(Hdtemplate bean) {
        bean.setModifytime(new Date());
        if(!Strings.isNullOrEmpty(bean.getYxtags())) bean.setYxtags(bean.getYxtags()
                .replace("；",";").replace("：",";"));
        boolean isSuccc = hdtemplateService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    @RequestMapping("/admin/hdtemplate/clone")
    public JsonDO clone(long id) {
        Hdtemplate bean = hdtemplateService.selectById(id);
        bean.setId(null);
        bean.setCreatetime(new Date());
        bean.insert();
        return new JsonDO();
    }

}
